#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#if __has_include(<AYBLECore/AYBLEDeviceType.h>)
#import <AYBLECore/AYBLEDeviceType.h>
#else
#import "AYBLEDeviceType.h"
#endif


NS_ASSUME_NONNULL_BEGIN

#define SharedBLEManager [AYBLEManager sharedManager]

extern void AYBLELogger(NSInteger level,NSString *log,...);

@interface AYBLEPacket : NSObject

/// 消息体部分
@property (nonatomic, copy) NSData *data;

@property (nonatomic, copy, nullable) NSDictionary *message;

/// 消息指令
@property (nonatomic, assign) AYBLECommandType command;

/// 是否超时
@property (nonatomic, assign) BOOL isTimeout;

+ (instancetype)packetWithCommand:(AYBLECommandType)command isTimeout:(BOOL)isTimeout data:(NSData *)data;

@end

typedef void(^AYBLECommandResponseBlock)(AYBLEPacket *response);

@class AYBLEDevice;
@interface AYBLEManager : NSObject

/// 连接状态回调:   -2：超时 -1：失败 0：成功
@property (nonatomic, copy, nullable) void(^connectStateBlock)(AYBLEDeviceConnectStatus connectState);

/// 断连回调
@property (nonatomic, copy, nullable) void(^disconnectStateBlock)(NSError *_Nullable error);
@property (nonatomic, copy, nullable) AYBLECommandResponseBlock commandResponseBlock;
@property (nonatomic, copy, nullable) void (^logCallback)(NSInteger level, NSString *log);
@property (nonatomic, strong, readonly, nullable) AYBLEDevice *device;
@property (nonatomic, assign, readonly) CBManagerState bleState;
@property (nonatomic, assign, readonly) BOOL isConnected;
@property (nonatomic, assign, readonly) BOOL isConnecting;

/// 单例对象
+ (instancetype)sharedManager;

/// 开始扫描
/// - Parameter prefixes: 扫描前缀过滤
- (void)startDiscover:(NSArray<NSString *> *)prefixes;

/// 停止扫描
- (void)stopDiscover;

/// 连接蓝牙设备（适用于蓝牙配网场景使用）
/// - Parameters:
///   - device: 需要连接的设备
///   - config: 蓝牙设备的相关配置
///   - encryptKey: 是否需要加密密钥
///   - timeout: 超时时间
///   - connectStateBlock: 连接状态回调
- (void)connectWithDevice:(AYBLEDevice *)device timeout:(double)timeout connectStateBlock:(void(^_Nullable)(AYBLEDeviceConnectStatus connectState))connectStateBlock;

/// 连接蓝牙设备（适用于进入设备详情场景使用）
/// - Parameters:
///   - MAC: 设备（mac地址）
///   - config: 蓝牙设备的相关配置
///   - encryptKey: 是否需要加密密钥
///   - timeout: 超时时间
///   - connectStateBlock: 连接状态回调
- (void)connectWithMAC:(NSString *)mac timeout:(double)timeout connectStateBlock:(void(^_Nullable)(AYBLEDeviceConnectStatus connectState))connectStateBlock;

/// 断开连接
- (void)disconnect;

/// 断开连接
/// - Parameter peripheral: 指定断开的设备
- (void)disconnect:(CBPeripheral *)peripheral;

- (void)sendCommand:(AYBLECommandType)command;
- (void)sendCommand:(AYBLECommandType)command timeout:(double)timeout;

- (void)sendCommand:(AYBLECommandType)command data:(nullable NSData *)data;
- (void)sendCommand:(AYBLECommandType)command data:(nullable NSData *)data timeout:(double)timeout;



/// completion
- (void)sendCommand:(AYBLECommandType)command completion:(nullable AYBLECommandResponseBlock)completion;
- (void)sendCommand:(AYBLECommandType)command timeout:(double)timeout completion:(nullable AYBLECommandResponseBlock)completion;


- (void)sendCommand:(AYBLECommandType)command data:(nullable NSData *)data completion:(nullable AYBLECommandResponseBlock)completion;

/// 发送指令
/// - Parameters:
///   - command: 指令枚举
///   - data: 需要携带的参数数据
///   - timeout: 超时时间
///   - completion: 结果回调
- (void)sendCommand:(AYBLECommandType)command data:(nullable NSData *)data timeout:(double)timeout completion:(nullable AYBLECommandResponseBlock)completion;


- (NSData *)xxteaEncrypt:(NSData *)data;
- (NSData *)xxteaDecrypt:(NSData *)data;



@end

NS_ASSUME_NONNULL_END
